﻿@typeparam T where T : notnull, Enum
<div class="jsinput">
    <div class="over-input">
        <Switch Value="@(Value!=null)" ShowInnerText="true" OffInnerText="默认" OnInnerText="自定义" OnValueChanged="OnValueChanged" ShowLabel="true" DisplayText="@DisplayText"></Switch>
    </div>

    @if (Value != null)
    {
        <MultiSelect @bind-Value="@SelectedEnumValues" OnSelectedItemsChanged="OnSelectedItemsChanged" />
    }
</div>

@code
{
    [CascadingParameter]
    [NotNull]
    public BigScreen? BigScreen { get; set; }
    [Parameter]
    public string DisplayText { get; set; } = "";
    [Parameter]
    public int Max { get; set; } = int.MaxValue;

    public List<T> SelectedEnumValues { get; set; } = new();
    /// <summary>
    ///
    /// </summary>
    [Parameter]
    public bool IsShow { get; set; }
    [Parameter]
    public JsVal? Value { get; set; }
    [Parameter]
    public EventCallback<JsVal?> ValueChanged { get; set; }

    public JsVal? ValueHander
    {
        get { return Value; }
        set
        {
            Value = value;
            if (ValueChanged.HasDelegate)
            {
                ValueChanged.InvokeAsync(Value);
            }
        }
    }

    protected override async Task OnInitializedAsync()
    {
        if (Value != null)
        {   //未实现,因为不好反序列化回来
            await JsEcharts.Log(Value.RAW);
            var sss = Value.RAW.Replace("[", "").Replace("]", "").Replace("\\", "").Replace("\"", "").Replace(" ", "");
            string[] stringValues = sss.Split(',');

            await JsEcharts.Log(sss);
            var enumType = typeof(T);
            foreach (var stringValue in stringValues)
            {
                if (Enum.TryParse(enumType, stringValue, out object? enumValue))
                {
                    SelectedEnumValues.Add((T)enumValue);
                }
            }
        }
        await base.OnInitializedAsync();
    }
    /// <summary>
    /// 仅仅在关闭的时候才会触发绘制
    /// </summary>
    /// <param name="res"></param>
    /// <returns></returns>
    async Task OnValueChanged(bool res)
    {
        if (res)
        {
            ValueHander = new JsVal("null") { JsType = JsType.String };
        }
        else
        {
            ValueHander = null;
            BigScreen?.RefreshHandel?.Invoke(true);
        }

        await Task.CompletedTask;
    }
    async Task OnValueChangedForRaw(string res)
    {
        if (Value != null)
        {
            switch (Value.JsType)
            {
                case JsType.Num:
                    if (!decimal.TryParse(res, out decimal num))
                    {
                        Value.RAW = "0";
                    }
                    break;
                case JsType.Object:
                    if (res.StartsWith("{") && res.EndsWith("}"))
                    {

                    }
                    else
                    {
                        Value.RAW = "{}";
                    }
                    break;
                case JsType.String:
                    break;
                case JsType.Function:
                    break;
                case JsType.Array:
                    if (res.StartsWith("[") && res.EndsWith("]"))
                    {

                    }
                    else
                    {
                        Value.RAW = "[]";
                    }
                    break;

                default:
                    break;
            }
        }
        BigScreen?.RefreshHandel?.Invoke(true);
        await Task.CompletedTask;
    }
    [Inject]
    [NotNull]
    public JsInterOp? JsEcharts { get; set; }
    async Task OnSelectedItemsChanged(IEnumerable<SelectedItem> items)
    {
        if (items.Count() > Max)
        {

        }
        else
        {
            // ValueHander = new JsFuncNumString() { JsType= JsType.Array};
            if (ValueHander != null)
            {

                ValueHander.RAW = JsonSerializer.Serialize(items.Select(x => x.Value));
                ValueHander.JsType = JsType.Array;
                ValueHander = new JsVal();
            }
        }
        await Task.CompletedTask;
    }

}

